package com.example.sangtangboot.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.sangtangboot.entity.CarAllResponse;
import com.example.sangtangboot.entity.CarCountResponse;
import com.example.sangtangboot.entity.CarInformationResponse;
import com.example.sangtangboot.entity.CarLibraryResponse;
import com.example.sangtangboot.exception.AppException;
import com.example.sangtangboot.resp.Resp;
import com.example.sangtangboot.service.CarService;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class CarController {

    @Resource
    private CarService carService;

    @GetMapping("/getVehicles")
    public Resp getVehicles(@RequestParam String sessionId, @RequestParam(name = "msg_id") String msgId,@RequestParam(name = "start_no") Integer startNo,@RequestParam(name = "lib_id") Integer libId)
    {
        String url = "http://192.168.1.101:80/api/json";  // 后端API地址
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("sessionid", sessionId);
        httpPost.setHeader("User-Agent", "Mozilla/5.0");

        JSONObject data = new JSONObject();
        data.put("msg_id", msgId);
        data.put("start_no", startNo);
        data.put("lib_id",libId);
        StringEntity entity = new StringEntity(data.toString(), "UTF-8");
        httpPost.setEntity(entity);
        JSONObject object = null;
        List<CarInformationResponse> list = new ArrayList<>();

        try {
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                object = JSONObject.parseObject(content);
                System.out.println(object.getIntValue("code"));
                if (object.getIntValue("code") == 0) {
                    System.out.println("data = " + object.get("data"));
                    System.out.println("我服了");
                    object = object.getJSONObject("data");
                    //Object data1 = object.get("data");
                    JSONArray records = object.getJSONArray("record");
                    //JSONArray records = (JSONArray) data1;
                    for(int i = 0; i < records.size(); i++){
                        JSONObject record = records.getJSONObject(i);
                        CarInformationResponse resp = carService.getVehicles(record);
                        list.add(resp);
                        System.out.println(resp);
                    }
                    System.out.println("object = " + object);
                } else {
                    //System.out.println("难道是我？");
                    throw new AppException(500, "请求无权限，请重新登录");
                }
            } else {
                throw new AppException(500, "请求失败");
            }
        } catch (IOException e) {
            //System.out.println("nainaide我错了");
            e.printStackTrace();
        }

        return Resp.success(list);
    }

    @GetMapping("/getGarage")
    public Resp getGarage(@RequestParam String sessionId, @RequestParam(name = "msg_id") String msgId)
    {
        String url = "http://192.168.1.101:80/api/json";  // 后端API地址
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("sessionid", sessionId);
        httpPost.setHeader("User-Agent", "Mozilla/5.0");

        JSONObject data = new JSONObject();
        data.put("msg_id", msgId);
        data.put("qry_len", 50); // 获取50条车辆库信息
        StringEntity entity = new StringEntity(data.toString(), "UTF-8");
        httpPost.setEntity(entity);
        JSONObject object = null;
        List<CarLibraryResponse> list = new ArrayList<>();
        //System.out.println("啊啊啊");
        try {
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                object = JSONObject.parseObject(content);
                if (object.getIntValue("code") == 0) {
                    //System.out.println("data = " + object.get("data"));
                    //System.out.println("我服了");
                    //object = object.getJSONObject("data");
                    Object data1 = object.get("data");
                    //JSONArray records = object.getJSONArray("record");
                    JSONArray records = (JSONArray) data1;
                    for(int i = 0; i < records.size(); i++){
                        JSONObject record = records.getJSONObject(i);
                        CarLibraryResponse resp = carService.getCarLibraries(record);
                        list.add(resp);
                        System.out.println(resp);
                    }
                    System.out.println("object = " + object);
                } else {
                    //System.out.println("难道是我？");
                    throw new AppException(500, "请求无权限，请重新登录");
                }
            } else {
                throw new AppException(500, "请求失败");
            }
        } catch (IOException e) {
            //System.out.println("nainaide我错了");
            e.printStackTrace();
        }

        return Resp.success(list);
    }



    @GetMapping("/getCar")
    public Resp get(@RequestParam String sessionId, @RequestParam(name = "msg_id") String msgId) {
        String url = "http://192.168.1.101:80/api/json";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("sessionid", sessionId);
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        JSONObject data = new JSONObject();
        data.put("msg_id", msgId);
        data.put("qry_len", 50);
        StringEntity entity = new StringEntity(data.toString(), "UTF-8");
        httpPost.setEntity(entity);
        JSONObject object = null;
        List<CarAllResponse> list = new ArrayList<>();
        try {
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                object = JSONObject.parseObject(content);
                if (object.getIntValue("code") == 0) {
                    object = object.getJSONObject("data");
                    JSONArray records = object.getJSONArray("record");
                    for(int i = 0; i < records.size(); i++){
                        JSONObject record = records.getJSONObject(i);
                        CarAllResponse resp = carService.convertCar(record);
                        list.add(resp);
                        System.out.println(resp);
                    }
                    System.out.println("object = " + object);
                } else {
                    throw new AppException(500, "请求无权限，请重新登录");
                }
            } else {
                throw new AppException(500, "请求失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Resp.success(list);
    }

    @GetMapping("/getCarCount")
    public Resp carCount(@RequestParam String sessionId, @RequestParam(name = "msg_id") String msgId) {
        String url = "http://192.168.1.101:80/api/json";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("sessionid", sessionId);
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        JSONObject data = new JSONObject();
        data.put("msg_id", msgId);
        data.put("qry_len", 50);
        StringEntity entity = new StringEntity(data.toString(), "UTF-8");
        httpPost.setEntity(entity);
        JSONObject object = null;
        List<CarCountResponse> respList = null;
        try {
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                object = JSONObject.parseObject(content);
                if (object.getIntValue("code") == 0) {
                    object = object.getJSONObject("data");
                    System.out.println("object = " + object);
                    respList = carService.count(object);
                } else {
                    throw new AppException(500, "请求无权限，请重新登录");
                }
            } else {
                throw new AppException(500, "请求失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Resp.success(respList);
    }
}
